package cn.wlinker.video.monitor.utils;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.thread.ThreadUtil;
import cn.wlinker.video.monitor.enums.FileTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 文件上传工具类
 *
 * @author gxsjx
 * @version 1.0
 * @date 2021/8/7
 * Copyright © goodits
 */
@Slf4j
public class FileUploadUtils {

    /**
     * 上传文件
     *
     * @param files
     * @return
     */
    public static List<String> upload(MultipartFile[] files) throws IOException {
        //上传目录地址

        List<String> urlPaths = new LinkedList<>();
        for (MultipartFile file : files) {
            FileTypeEnum fileTypeEnum = FileTypeEnum.getByFileName(file.getOriginalFilename());
            String uploadDir = FilePathUtils.getUploadDir(fileTypeEnum);
            Assert.notBlank(uploadDir, "获取上传目录失败");
            Assert.notNull(file, "文件不能为空");
            Assert.notEmpty(file.getOriginalFilename(), "文件不能为空");
            String suffix;
            String fileName;
            FileOutputStream fileOutputStream = null;
            InputStream inputStream = null;
            try {
                //调用上传方法
                //后缀名
                suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));
                fileName = UUID.fastUUID() + suffix;
                String fullFileName = uploadDir + fileName;
                fileOutputStream = new FileOutputStream(fullFileName);
                inputStream = file.getInputStream();
                IOUtils.copy(file.getInputStream(), fileOutputStream);
                String urlPathByFileName = FilePathUtils.getUrlPathByFileName(fileName);
                urlPaths.add(urlPathByFileName);
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }
        }
        return urlPaths;
    }

    /**
     * 删除文件
     *
     * @param filePath
     * @return
     */
    public static Boolean delete(String filePath) {
        File file = new File(filePath);
        Assert.isTrue(file.exists(), "文件不存在");
        return file.delete();
    }

    /**
     * 删除文件
     *
     * @param filePath
     * @return
     */
    public static void asyncDelayDelete(String filePath) {
        ThreadUtil.execute(() -> {
            try {
                Thread.sleep(1000 * 3);
                File file = new File(filePath);
                Assert.isTrue(file.exists(), "文件不存在");
                file.delete();
            } catch (InterruptedException e) {
                log.error("删除文件异常", e);
            }
        });
    }

}
